home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Evaluate / Evaluate.h next >
Text File  |  1994-05-28  |  5KB  |  123 lines

  1. /****************************************************************************************
  2. *
  3. *
  4. *    Evaluate.h            - header file for Evaluate.Lib
  5. *
  6. *    ©1994, Graham Cox. All Rights Reserved.
  7. *
  8. *    May 16th, 1994
  9. *
  10. *
  11. ****************************************************************************************/
  12.  
  13. /*
  14.  
  15. This library is fairly complex and contains many functions. However, you only need one of
  16. them to use it- Evaluate. This function takes a pascal string and a value, and returns
  17. the result of the expression in the string. It also returns an error code which is one
  18. of the error values listed below, or noErr. The parser looks up tokens in a resource of
  19. type 'TOKN', which is hard coded in the library to have the ID 128, The following
  20. constants are provided for your convenience, but changing them will NOT result in the
  21. library using a different reource type or ID. If you wish to look up a string corresponding
  22. to the error code, subtract 100 from the code and use it as an index into the STR#
  23. resource which is also provided with the ID given below. This ID may be changed to fit
  24. your app's resource ID numbering because this is not used by this library.
  25.  
  26. */
  27.  
  28.  
  29.  
  30.  
  31. #define        tokenTableResType    'TOKN'
  32. #define        tokenTableResID        128
  33.  
  34. #define        SyntaxError            100
  35. #define        LRParenError        101
  36. #define        IllegalTokErr        102
  37. #define        ParseError            103
  38. #define        endOfLine            104
  39. #define        OutOfMemory            105
  40. #define        ItsAllGoneWrong        106
  41. #define        NoTokenTable        107
  42.  
  43. #define        ErrorStringListID    130
  44.  
  45. /* globals */
  46.  
  47. extern int        CompileError;        // global error flag
  48. extern int        gCurrentMark;        // current character being scanned
  49.  
  50.  
  51.  
  52. /*
  53.  
  54. Function Prototype
  55.  
  56. */
  57.  
  58. OSErr    Evaluate(Str255 *inputStr,extended *value);
  59.  
  60.  
  61. /*
  62.  
  63. To call this function, create a pascal string containing the expression to be evaluated.
  64. e.g. myExpression = "\p1.5*cos(x)+sin(x/2)" and pass its address in inputStr. Pass the
  65. value of x in value. All expressions should be in terms of x- other variables can be
  66. inserted but they will be automatically evaluated at a value of zero. There is no easy
  67. way to insert values for these variables, though the library supports this.
  68.  
  69. How it works: The expression parsed from left to right and is converted to Reverse Polish
  70. aka Postfix notation. This notation is stored internally in a queue structure. The RPN
  71. queue is then evaluated using an evaluation stack. Errors can be encountered in either
  72. stage which aborts the process and returns the error code. The internal structures are
  73. created and destroyed completely internally- there is no action you need to take to 
  74. manage this. Be aware however, that these internal structures can grow large if an
  75. expression is very complex, so be sure to give your app enough memory.
  76.  
  77. The prototypes of other internal routines are listed here for completeness. You should not
  78. need to call them- if you do you are on your own. They are listed here because they are
  79. not private, and so if you get a linker conflict with your own functions, this may allow
  80. you to identify the culprit!
  81.  
  82.  
  83. QSHandle    MakeRPNQueue(Str255* inString);
  84. extended**    MakeFPObject(extended fpData);
  85. varEntryHdl    MakeVarObject(extended fpData,char* vName);
  86. varTabHdl     MakeVariableTable(QSHandle rpnQueue);
  87. QSHandle    CloneQSStruct(QSHandle src);
  88. extended    EvaluateRPN(QSHandle rpnQueue,varTabHdl vTab);
  89.  
  90. QSHandle    NewQSStruct(int sqType);        
  91. void        DisposeQSStruct(QSHandle theQS);
  92. QSElemHdl    NewQSItem(int qsType,long qsData);
  93. void        DisposeQSItem(QSElemHdl theItem);
  94. void        PushQSItem(QSElemHdl theItem,QSHandle theQS);
  95. QSElemHdl    PopQSItem(QSHandle    theQS);
  96. QSElemHdl    PeekQSItem(QSHandle    theQS);
  97. Boolean        EmptyQSStruct(QSHandle theQS);
  98. long        QSLength(QSHandle    theQS);
  99.  
  100. tkTabHdl    GetTokenTable(int resourceID);
  101. Boolean        IsBinOp(char theChar);
  102. Boolean     IsBracket(char theChar);
  103. Boolean        LegalPair(char firstChar,char secondChar);
  104. int            GetNextRawToken(char* inString,char* outString,int *sectLength);
  105. void        RemoveWhiteSpace(char* theString);
  106. int            GetToken(tkTabHdl ttTable,char* rawString,tEntry* theToken);
  107. void        RPNConversion(QSElemHdl qItem,QSHandle stack,QSHandle queue);
  108. void        ReadRPNString(QSHandle rpnQueue,Str255* theString);
  109.  
  110.  
  111. This library uses the THINK C implementation of SANE- you should include <Sane.h> in your
  112. project and the SANE library. It deals with all floating point values as extended types,
  113. and you should compile your project with 'Native Floating Point Format' ON. It also relies
  114. on MacTraps.
  115.  
  116. The header files of the components which make up this library are also supplied- in general
  117. there is no need to include them- just this one will do.
  118.  
  119. */
  120.  
  121.  
  122.  
  123.